//
//  ERClass.m
//  metadata
//
//  Created by easy on 11-5-30.
//  Copyright 2011 zeasy@qq.com. All rights reserved.
//
#import <objc/objc-runtime.h>
#import "ERClass.h"
#import "ERClassAttributes.h"


@implementation ERClass
@synthesize source;
@synthesize attributes;
@synthesize instanceSize;
@synthesize name;
@synthesize version;
@synthesize superClazz;
#pragma mark init
-(id) initWith:(Class) aClass{
	if (self = [super init]) {
		source = aClass;	//can't retain 
	}
	return self;
}
+(id) classWith:(Class) aClass{
	return [[[ERClass alloc] initWith:aClass] autorelease];
}
 
#pragma mark create instance
-(id) newInstance:(NSUInteger) extraBytes{
	return class_createInstance(self.source, extraBytes);
}
-(id) newInstance{
	return [self newInstance:0];
}
+(BOOL) isSuperClass:(Class) superClazz sub:(Class) subClazz{
	if (!subClazz) {
		return NO;
	}
	Class subSuper = class_getSuperclass(subClazz);
	if (subSuper == superClazz) {
		return YES;
	}
	return [ERClass isSuperClass:superClazz sub:subSuper];
}
#pragma mark field
-(ERField *) instanceFieldWithName:(NSString *) fieldName{
	return [self.attributes instanceFieldWithName:fieldName];
}
-(ERField *) classFieldWithName:(NSString *) fieldName{
	return [self.attributes classFieldWithName:fieldName];
}
-(NSArray *) allFields{
	return [self.attributes allFields];
}
-(NSArray *) allFieldNames{
	return [self.attributes allFieldNames];
}
#pragma mark method
-(ERMethod *) instanceMethodWithName:(NSString *) methodName{
	return [self.attributes instanceMethodWithName:methodName];
}
-(ERMethod *) classMethodWithName:(NSString *) methodName{
	return [self.attributes classMethodWithName:methodName];
}
-(NSArray /*ERMethod*/*) allMethods{
	return [self.attributes allMethods];
}
-(NSArray /*NSString*/*) allMethodNames{
	return [self.attributes allMethodNames];
}
#pragma mark property
-(ERProperty *) propertyWithName:(NSString *) propertyName{
	return [self.attributes propertyWithName:propertyName];
}
-(NSArray /*ERProperty*/*) allProperties{
	return [self.attributes allProperties];
}
-(NSArray /*NSString*/*) allPropertyNames{
	return [self.attributes allPropertyNames];
}
#pragma mark property setter/getter
-(ERClassAttributes *) attributes{
	if (!attributes) {
		ERClassAttributes *attrs = [[ERClassAttributes alloc] initWithERClass:self];
		attributes = [attrs retain];
		[attrs release];
	}
	return attributes;
}
-(NSString *) name{
	return self.attributes.name;
}
-(NSUInteger) instanceSize{
	return self.attributes.instanceSize;
}
-(int) version{
	return self.attributes.version;
}
-(ERClass *) superClazz{
	return self.attributes.superClazz;
}
-(NSString *) iverLayout{
	return self.attributes.iverLayout;
}
-(NSString *) weakIverLayout{
	return self.attributes.weakIverLayout;
}

-(void) dealloc{
	source = nil;
	[attributes release];
	[super dealloc];
}
@end
